import { h, ref } from 'vue';
import { NTag } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import {createStorage} from "@/utils/Storage";
import {LATEST_DAY} from "@/store/mutation-types";

const storage = createStorage({ storage: localStorage });
let latestDay = Date.parse(storage.get(LATEST_DAY))

export class State {
  public id = 0; // id
  public code = ''; // code
  public name = ''; // name
  public turnover = null; // 成交额
  public date = ''; // 日期
  public desc = ''; // 解读
  public sellSeat = null; // sell_seat
  public buySeat = null; // buy_seat
  public lastPrice = null; // 最新价
  public changePercent = null; // 涨跌幅
  public circulatingMarketCap = null; // 流通市值
  public marketCap = null; // 总市值
  public turnoverRate = null; // 换手率

  public fbzj = null; // 封板资金

  public zbcs = 0; // 炸板次数
  public lbNumber = 0; // 连板次数

  public ztStatisticsDays = 0; // 涨停统计-天数范围
  public ztStatisticsNum = 0; // 涨停统计-涨停天数

  public industry = ''; // 所属行业
  public firstFbTime = ''; // 首次封板时间
  public lastFbTime = ''; // 最后封板时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

export const rules = {
  code: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入code',
  },
  turnover: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入成交额',
  },
  date: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入日期',
  },
};

export const schemas = ref<FormSchema[]>([
  {
    field: 'date',
    component: 'NDatePicker',
    label: '日期',
    defaultValue:latestDay,
    componentProps: {
      placeholder: '日期',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

export const columns = [
  {
    title: '代码',
    key: 'code',
    width: 80,
  },
  {
    title: '名字',
    key: 'name',
    width: 80,
  },
  {
    title: '成交额',
    key: 'turnover',
    width: 100,

    render: (row: any) => {
      const num = Number(row.turnover);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '最新价',
    key: 'lastPrice',
    width: 80,
  },
  {
    title: '涨跌幅',
    key: 'changePercent',
    width: 80,
    render: (row: any) => {
      return h(
        NTag,
        {
          type: row.changePercent < 0 ? 'success' : 'error',
        },
        {
          default: () => row.changePercent + '%',
        }
      );
    },
  },
  {
    title: '流通市值',
    key: 'circulatingMarketCap',
    width: 100,

    render: (row: any) => {
      const num = Number(row.circulatingMarketCap);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '总市值 ',
    key: 'marketCap',
    width: 100,

    render: (row: any) => {
      const num = Number(row.marketCap);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '换手率',
    key: 'turnoverRate',

    render: (row: any) => {
      const num = Number(row.turnoverRate);
      return num + '%';
    },
  },
  {
    title: '封板资金',
    key: 'fbzj',
    width: 100,

    render: (row: any) => {
      const num = Number(row.fbzj);
      if (num == 0 || (num > 0 && num < 10000)) {
        return num + '';
      } else if (num == 0 || (num > 10000 && num < 10000 * 10000)) {
        return (num / 10000).toFixed(2) + '万';
      } else {
        if (num / (10000 * 10000) < 0.1) {
          return (num / 10000).toFixed(2) + '万';
        } else {
          return (num / (10000 * 10000)).toFixed(2) + '亿';
        }
      }
    },
  },
  {
    title: '炸板次数',
    key: 'zbcs',
  },
  {
    title: '涨停统计',
    key: 'ztStatisticsDays',
    render: (row: any) => {
      return row.ztStatisticsDays + '天' + row.ztStatisticsNum + '板';
    },
  },
  {
    title: '所属行业',
    key: 'industry',
  },
  {
    title: '首次封板时间',
    key: 'firstFbTime',
  },
  {
    title: '最后封板时间',
    key: 'lastFbTime',
  },
];
